﻿/// <reference path="jquery-1.4.1.js" />
function calculateRisk(freq, exp, cont, sev, kfactor, prob, level, importance, term, expiration, actual, newval, newlevel) {
    var frecuenceVal = parseInt($(freq).val());
    var exposedVal = parseInt($(exp).val());
    var controlsVal = parseInt($(cont).val());
    var severityVal = parseInt($(sev).val());
    var frequence;

    switch (frecuenceVal) {
        case 1:
            frequence = 10;
            break;
        case 2:
            frequence = 30;
            break;
        case 3:
            frequence = 50;
            break;
        case 4:
            frequence = 70;
            break;
        case 5:
            frequence = 100;
            break;
        default:
            frequence = 0;
            break;
    }

    var exposed;

    switch (exposedVal) {
        case 1:
            exposed = 30;
            break;
        case 2:
            exposed = 60;
            break;
        case 3:
            exposed = 100;
            break;
        default:
            exposed = 0;
            break;
    }

    var controls;
    switch (controlsVal) {
        case 1:
            controls = 20;
            break;
        case 2:
            controls = 70;
            break;
        case 3:
            controls = 100;
            break;
        default:
            controls = 0;
            break;
    }

    var probability = frequence * 0.4 + exposed * 0.3 + controls * 0.3;
    var probabilityText = "";
    var risklevel = 0;
    var levelArray = new Array(new Array(1, 3, 3, 4, 4),
                                new Array(1, 2, 3, 3, 4),
                                new Array(0, 1, 2, 3, 3),
                                new Array(0, 0, 1, 2, 3),
                                new Array(0, 0, 1, 1, 2));
    var levelList = new Array("<span style=\"color: green; font-size: 150%;\">Riesgo Bajo</span>",
                                "<span style=\"color: green; font-size: 150%;\">Riesgo Moderado</span>",
                                "<span style=\"color: yellow; font-size: 150%;\">Riesgo Importante</span>",
                                "<span style=\"color: red; font-size: 150%;\">Riesgo Alto</span>",
                                "<span style=\"color: red; font-size: 150%;\">Riesgo Intolerable</span>");
    var termList = new Array("Incluir en la mejora a largo Plazo",
                            "Acciones hasta 3 años",
                            "Acciones en 1 año",
                            "Acciones en 3 meses",
                            "Paro, Acciones Inmediatas");
    var riskValues = new Array(2.4, 3.4, 4.9, 7, 10);

    var expDate = new Date();

    if (probability < 35) {
        probabilityText = "<span style=\"color: green; font-size: 150%;\">Muy baja</span>";
        risklevel = levelArray[4][severityVal - 1];        
    } else if (probability < 51.1) {
        probabilityText = "<span style=\"color: green; font-size: 150%;\">Baja</span>";
        risklevel = levelArray[3][severityVal - 1];        
    } else if (probability < 67.1) {
        probabilityText = "<span style=\"color: yellow; font-size: 150%;\">Media</span>";
        risklevel = levelArray[2][severityVal - 1];
    } else if (probability < 83.1) {
        probabilityText = "<span style=\"color: red; font-size: 150%;\">Alta</span>";
        risklevel = levelArray[1][severityVal - 1];        
    } else {
        probabilityText = "<span style=\"color: red; font-size: 150%;\">Muy Alta</span>";
        risklevel = levelArray[0][severityVal - 1];
    }

    switch (risklevel) {
        case 4:
            expDate.setDate(expDate.getDate() + 1);
            break;
        case 3:
            expDate.setMonth(expDate.getMonth() + 3);
            break;
        case 2:
            expDate.setFullYear(parseInt(expDate.getFullYear()) + 1);
            break;
        case 1:
            expDate.setFullYear(parseInt(expDate.getFullYear()) + 3);
            break;
        case 0:
            expDate.setFullYear(parseInt(expDate.getFullYear()) + 10);
            break;   
    }
    var significance = risklevel >= 2 ? "<span style=\"color: red; font-size: 150%;\">SI</span>" : "NO";
    var thenewVal = riskValues[risklevel] * parseFloat($(kfactor).val()) / 10;
    var thenewlevel = 0;

    if (thenewVal <= 2.4)
        thenewlevel = 0;
    else if (thenewVal > 2.4 && thenewVal <= 3.4)
        thenewlevel = 1;
    else if (thenewVal > 3.4 && thenewVal <= 4.9)
        thenewlevel = 2;
    else if (thenewVal > 4.9 && thenewVal <= 7)
        thenewlevel = 3;
    else if (thenewVal > 7 && thenewVal <= 10)
        thenewlevel = 4;
    else
        thenewlevel = "Error"; 
    $(prob).html(probabilityText);
    $(level).html(levelList[risklevel]);
    $(importance).html(significance);
    $(term).html(termList[risklevel]);
    $(expiration).val(expDate.getDate() + "/" + (expDate.getMonth() + 1) + "/" + expDate.getFullYear());
    $(actual).html(levelList[risklevel]);
    $(newval).html(thenewVal);
    $(newlevel).html(levelList[thenewlevel]);
}